package com.yc.hundun.server;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：冉野
 * @date ：Created in 2021-07-15 18:29
 * @description：
 * @modified By：
 * @version: $
 */
public class Servers {

    public static ConcurrentHashMap<String, HashMap<String, ServerDomain>> servers = new ConcurrentHashMap();


    public static void put(ServerDomain serverDomain) {
        String serverId = serverDomain.getServerId();
        final HashMap<String, ServerDomain> serverDomains = servers.get(serverId);

        if (Objects.isNull(serverDomains)) {

            HashMap<String, ServerDomain> serverDomainHashMap = new HashMap<>();
            serverDomainHashMap.put(serverDomain.getAddress(), serverDomain);
            servers.put(serverId, serverDomainHashMap);
        } else {
            final ServerDomain serverDomain1 = serverDomains.get(serverDomain.getAddress());
            if (Objects.nonNull(serverDomain1)) {
                serverDomain1.setLastHeartBeat(System.currentTimeMillis());
            } else {
                serverDomains.put(serverDomain.getAddress(), serverDomain);
            }
        }
    }

    public static HashMap<String, ServerDomain> get(String serverId) {
        return servers.get(serverId);
    }


    public static ConcurrentHashMap<String, HashMap<String, ServerDomain>> getAll() {

        return servers;
    }

}
